package cn.bouncyslime.graduationProject.nba.test;

import cn.bouncyslime.graduationProject.nba.dto.PlayerInfoDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 张一卓
 * @date 2020/4/1 16:07
 **/
public class PlayerInfoTest2 implements PageProcessor {
    private static final Logger logger = LoggerFactory.getLogger(PlayerInfoTest.class);

    private static final String BASE_URL = "http://www.stat-nba.com/playerList.php";

    private static final String START_URL = "http://www.stat-nba.com/playerList.php";

    private static String regex = "\\.(/([a-zA-Z]+)/(\\d+).([a-zA-Z]+))";

    private static String heightRegex = "([\\d\\.]+)米\\((\\d+尺\\d+寸)\\)";

    private static String weightRegex = "([\\d]+)公斤\\(([\\d]+)磅\\)";

    private static String salaryRegex = "(\\d+)(\\S+)";

    private static Pattern pattern = Pattern.compile(regex);

    private static Pattern heightPattern = Pattern.compile(heightRegex);

    private static Pattern weightPattern = Pattern.compile(weightRegex);

    private static Pattern salaryPattern = Pattern.compile(salaryRegex);

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");

    private Site site = Site.me().setSleepTime(3000).setRetryTimes(3).setRetrySleepTime(1000).setCharset("utf-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.369")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
            .addHeader("Connection", "keep-alive")
            .addHeader("Host", "www.stat-nba.com")
            .addCookie("__cfduid", "def62db9852e6b626f3d744bacadd235a1585654597")
            .addCookie("Hm_lvt_102e5c22af038a553a8610096bcc8bd4", "1585654597")
            .addCookie("yjs_id", "aHR0cHM6Ly93d3cuYmFpZHUuY29tL2xpbms/dXJsPXlGVU92Y0NMMERvR3ZUT01SVm1kS05NeExSeDNEWHFxcklfakxDSXVpMHJxY1E0RkJITTFncEhGX2lQcHJ2ekgmd2Q9JmVxaWQ9ZGM3OTgyN2YwMDczMjg1OTAwMDAwMDAzNWU4MzJiNDN8MTU4NTY1NDU5Njg0Ng")
            .addCookie("ctrl_time", "1")
            .addCookie("Hm_lpvt_102e5c22af038a553a8610096bcc8bd4", "1585672286");

    @Override
    public void process(Page page) {
        String pageUrl = page.getUrl().toString();
        if (page.isDownloadSuccess()) {
            Html html = page.getHtml();
            // 当前爬取页面是 球员列表页面时
            if (pageUrl.contains(START_URL)) {

                Selectable selectable = html.xpath("div[@class='playerList']");
                logger.debug(String.valueOf(selectable.match()));
                List<String> playerIdList;
                if (!selectable.match()) {
                    logger.debug(String.valueOf(selectable));
                }else {
                    int nodeIndex = 0;
                    if (selectable.nodes().size() == 1){
                        nodeIndex = 1;
                    }else if (selectable.nodes().size() == 2){
                        nodeIndex = 2;
                    }
                    playerIdList = selectable.nodes().get(nodeIndex).xpath("div[@class='name']/a/@href").all();

                }

//                List<String> playerIdList = html.xpath("div[@class='playerList']").nodes().get(1)
//                        .xpath("div[@class='name']/a/@href").all();
            }
        } else {
            logger.warn("" + pageUrl + "页面爬取失败");
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new PlayerInfoTest2()).addUrl("http://www.stat-nba.com/playerList.php?il=0&lil=X")
                .addPipeline(new ConsolePipeline())
                .thread(5).run();
    }
}
